// index.js
Page({
  data: {
    snowflakes: [],
    maxSnowflakes: 300, // 雪花数量
    screenWidth: 0,
    screenHeight: 0,
  },
  onLoad: function() {
    this.initializeScreenSize();
    this.createSnowflakes();
  },
  onReady: function() {
    this.startAnimatingSnowflakes();
  },
  initializeScreenSize: function() {
    const { windowWidth, windowHeight } = wx.getSystemInfoSync();
    this.setData({
      screenWidth: windowWidth,
      screenHeight: windowHeight,
    });
  },
  createSnowflakes: function() {
    const snowflakes = [];
    const { screenWidth } = this.data;
    for (let i = 0; i < this.data.maxSnowflakes; i++) {
      snowflakes.push({
        uniqueId: `flake-${i}`,
        x: Math.random() * screenWidth,
        y: Math.random() * this.data.screenHeight, // 随机初始位置
        speed: 1, // 速度范围1到5
      });
    }
    this.setData({ snowflakes });
  },
  startAnimatingSnowflakes: function() {
    const that = this;
    const animate = () => {
      const { snowflakes, screenWidth, screenHeight } = that.data;
      const updatedSnowflakes = snowflakes.map((snowflake) => {
        const newY = snowflake.y + snowflake.speed;
        if (newY > screenHeight) {
          return {
            ...snowflake,
            x: Math.random() * screenWidth,
            y: -20,
          };
        }
        return {
          ...snowflake,
          y: newY,
        };
      });
      that.setData({ snowflakes: updatedSnowflakes });
      setTimeout(animate, 30); // 约33帧/秒
    };
    animate();
  },
});